#region Summary

// 
// Owner : JackieHan 
// 
// Solution : LearnSolution
// 
// Project : JackieHan.DotNet.Expression
// 
// File : ExpressionExtension.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 表达式扩展
// 
// History : 
//			2010-10-22 13:12:53	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

#endregion

namespace JackieHan.DotNet.LinQExpression
{
    /// <summary> 
    /// 表达式扩展
    /// </summary>
    public static class ExpressionExtension
    {
        public static Expression<Func<T,bool>> Ture<T>()
        {
            return f => true;
        }

        public static Expression<Func<T,bool>> False<T>()
        {
            return f => false;
        }

        public static Expression<Func<T,bool>> Or<T>(this Expression<Func<T,bool>> expression,Expression<Func<T,bool>> expression2)
        {
            var invokeExpression = Expression.Invoke(expression2, expression.Parameters.Cast<Expression>());

            return Expression.Lambda<Func<T, bool>>(Expression.Or(expression.Body, invokeExpression),
                                                    expression.Parameters);
        }

        public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
        {
            var invokeExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

            return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expression1.Body, invokeExpression),
                                                    expression1.Parameters);
        }
    }
}